<template>
	<view class="mainbgc">
		<header-top :messageInfo="{typeFlag:true,message:'仓库档案',liner:false}">
			<view class="rightmsg" slot="rightmsg">
				<view class="rightmsg_content flexright">
					<view class="add" @click="jumpurl('/pages/workbench/jiben/cangkudaadd',15)"></view>
				</view>
			</view>
		</header-top>
		<view class="mainpadding">
			<view class="flexbetween">
				<view class="flexbetween searchbox margin_right">
					<input type="text" placeholder="输入仓库名" v-model="keyword" @confirm="init()">
					<view class="searchimg" @click="init()"></view>
				</view>
				<view class="flexbetween searchbox margin_right" @click="ckPianQuShow = true">
					<input type="text" placeholder="点击选择片区" disabled="true" v-model="ckPianQuText">
					<view class="righticon2"></view>
				</view>
				<view class="flexbetween searchbox margin_right" @click="ckBuMenShow = true">
					<input type="text" placeholder="点击选择部门" disabled="true" v-model="ckBuMenText">
					<view class="righticon2"></view>
				</view>
			</view>
			<view class="flexcenter" v-if="!listtj.total">
				<view class="placeholderimage"></view>
			</view>
			<template v-if="listtj.total">
				<view class="margin_top strongtext">共计：{{listtj.total}}个</view>
				<view class="mainpadding_top ffffff radius" v-for="item in listtj.data" :key="item.id" @click="selectRole(item)">
					<view class="flexbetween">
						<view class="titletext">{{item.name}}</view>
						<view class="strongtext">{{item.warehouse_no}}</view>
					</view>
					<view class="flexbetween margin_top">
						<view class="">
							<view class="flexbetween">
								<view class="smalltext">负责区域：{{item.fz_area}}</view>
								<view class="smalltext" style="color: #ffa126;" v-if="item.is_pd">盘点中</view>
							</view>
							<view class="smalltext margin_top">负责人：{{item.staff.username}} {{item.staff.mobile}}</view>
							<view class="smalltext margin_top">所属部门：{{item.staffbm.name}}</view>
							<view class="flexleft margin_top">
								<view class="smalltext xiankuan">地址：{{item.address}}</view>
								<!-- <view class="addresstext">{{kmUnit(Number(item.distance))}}</view> -->
							</view>
						</view>
						<view class="flexright">
							<view class="addressicon margin_right" @click.stop="openmap(item)"></view>
							<view class="phoneicon" @click.stop="phone(item.staff.mobile)"></view>
						</view>
					</view>
					<view class="flexright margin_top">
						<view class="xiaolankuang flexbetween margin_left2" @click.stop="edit('/pages/workbench/jiben/cangkudaadd?id=',item.id,16)">
							<u-icon name="edit-pen" color="#0BBDCB" size="16"></u-icon>
							<view class="xiaolan margin_left1">编辑</view>
						</view>
						<view class="xiaolankuang flexbetween margin_left2" @click.stop="delItem(item.id,17)">
							<u-icon name="trash" color="#0BBDCB" size="16"></u-icon>
							<view class="xiaolan margin_left1">删除</view>
						</view>
					</view>
				</view>
			</template>
		</view>
		<selectCKBuMen :message="{ckBuMenShow:ckBuMenShow}" @ckBuMenSure="ckBuMenSure"></selectCKBuMen><!-- 仓库部门 -->
		<selectCKPianQu :message="{ckPianQuShow:ckPianQuShow}" @ckPianQuSure="ckPianQuSure"></selectCKPianQu><!-- 仓库片区 -->
		<moDal :message="{modalShow:modal.modalShow,title:modal.title}" @modalSure="modalSure"></moDal><!-- 模态框 -->
		
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				// 模态框
				modal:{
					modalShow:false,
					title:"",
				},
				currentId:"",
				// 仓库片区
				ckPianQuShow: false,
				ckPianQuText: "",
				staff_area_id: "", //仓库片区id
				// 仓库部门
				ckBuMenShow: false,
				ckBuMenText: "",
				staff_bm_id: "", //仓库部门id
				keyword: '',
				page: 1,
				limit: 12,
				listtj: {
					data: []
				},
				pianqutext: "",
				// 片区
				pianqu: false,
				columnspianqu: [],
				pianquid: "",
				lat: "35.17466",
				lng: "109.67307",
				type:"",
				
			}
		},
		onShow() {
			this.init()
		},
		onLoad(options) {
			if (options.type) {
				this.type = options.type
			}
		},
		methods: {
			selectRole(item) {
				if(this.type==1){
					let e = {
						id: item.id,
						name: item.name,
						mobile:item.staff.mobile
					}
					uni.$emit("ckSelectFn",e)
					uni.navigateBack(1)
				}else{
					this.edit('/pages/workbench/jiben/cangkudaadd?id=',item.id,16)
				}
			},
			delItem(id,qxpdid) {
				if (!httpRequest.qxpd(qxpdid, this.$store.state.userInfo.ability_rule_ids)) {
					return false
				}
				this.modal.modalShow = true
				this.modal.title = "确定删除当前内容吗"
				this.currentId = id
			},
			modalSure(){
				httpRequest.request('/dhtapi/Warehouse/deleteWarehouse', 'POST', {
					id: this.currentId
				}, false, false, true).then(res => {
					httpRequest.toast(res.msg)
					if(res.code==1){
						this.init()
					}
				})
			},
			ckBuMenSure(e) { //仓库部门
				if (e) {
					this.ckBuMenText = e.value[0].name
					this.staff_bm_id = e.value[0].id
				}
				this.ckBuMenShow = false
				this.init()
			},
			ckPianQuSure(e) { //仓库片区
				if (e) {
					this.ckPianQuText = e.value[0].name
					this.staff_area_id = e.value[0].id
				}
				this.ckPianQuShow = false
				this.init()
			},
			// 获取当前位置信息
			getlocation() {
				let _this = this
				uni.showLoading({
					title:"请稍候"
				})
				uni.getLocation({
					type: 'gcj02',
					geocode: true, //设置该参数为true可直接获取经纬度及城市信息
					success: function(res) {
						console.log("获取经纬度信息", res)
						_this.lat = res.latitude
						_this.lng = res.longitude

						_this.init()
					},
					fail(err) {
						uni.hideLoading()
						console.log(err, "定位错误信息")
					}
				});
			},
			init(isPage, page) {
				let _this = this;
				httpRequest.request('/dhtapi/Warehouse/index', 'POST', {
					page: page || 1,
					limit: _this.limit,
					keyword: _this.keyword,
					lng: this.lng,
					lat: this.lat,
					staff_area_id: this.staff_area_id, //片区id
					staff_bm_id: this.staff_bm_id
				}, false, false, true).then(res => {
					console.log(res, 6363);
					if (res.code == 1) {
						let resList = res.data.data;
						if (isPage == true) {
							_this.listtj.data = _this.listtj.data.concat(resList);
							// _this.listtj.data.push(...resList);
						} else {
							_this.listtj = res.data
							_this.listtj.data = resList;
						}
					}
					uni.hideLoading()
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			onReachBottom() {
				let _this = this;
				let listtj = _this.listtj;
				let page = _this.page;
				if (listtj.last_page <= page) {
					return false;
				}
				page++;
				_this.page = page;
				_this.init(true, page);
			},
			jumpurl(url,qxpdid) {
				if (!httpRequest.qxpd(qxpdid, this.$store.state.userInfo.ability_rule_ids)) {
					return false
				}
				uni.navigateTo({
					url: url
				})
			},
			edit(url,val,qxpdid) {
				if (!httpRequest.qxpd(qxpdid, this.$store.state.userInfo.ability_rule_ids)) {
					return false
				}
				uni.navigateTo({
					url:url+val
				})
			},
			// 米，公里转换
			kmUnit(m) {
				var v;
				if (typeof m === 'number' && !isNaN(m)) {
					if (m >= 1000) {
						v = (m / 1000).toFixed(2) + 'km'
					} else {
						v = m + 'm'
					}
				} else {
					v = '0m'
				}
				return v;
			},
			// 打开地图
			openmap(item) {
				uni.openLocation({
					latitude: Number(item.latitude), //即将传到高德或腾讯地图的终点纬度  必须是数值，字符串无效
					longitude: Number(item.longitude), //即将传到高德或腾讯地图的终点经度  必须是数值，字符串无效
					name: item.name, //即将传到高德或腾讯地图的店铺名称
					address: item.address, //即将传到高德或腾讯地图的详细地址
				})
			},
			phone(p) {
				uni.makePhoneCall({
					phoneNumber: p.toString(),
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	// 顶部搜索
	.searchimg {
		width: 24rpx;
		height: 24rpx;
		background-image: url("@/static/image/system/search.png");
		background-size: 100% 100%;
	}

	.searchbox {
		width: 35%;
		padding: 12rpx 20rpx;
		box-sizing: border-box;
		background-color: #f3f3f3;
		border-radius: 50rpx;

		input {
			width: 80%;
			font-size: 24rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
		}
	}

	.smalltext {
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #737373;
	}
</style>